package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.UserCreditApplyService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.UserCreditApplyDao;
import com.qdlc.p2p.dal.dto.UserCreditApply;
import com.qdlc.p2p.dal.model.BorrowModel;
import com.qdlc.p2p.dal.model.UserCreditApplyModel;

@Service("userCreditApplyService")
public class UserCreditApplyServiceImpl implements UserCreditApplyService {

    @Resource
    private UserCreditApplyDao userCreditApplyDao;

    @Override
    public int count(long userId) {
        return userCreditApplyDao.count(userId);
    }

    @Override
    public int count(long userId, int status) {
        return userCreditApplyDao.count(userId, status);
    }

    @Override
    public PageDataList<UserCreditApply> list(long userId, BorrowModel model) {
        QueryParam param = QueryParam.getInstance().addParam("user.userId", userId).addOrder(OrderType.DESC, "addTime")
                .addPage(model.getPage());
        if (StringUtil.isNotBlank(model.getStartTime())) {
            Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
            param.addParam("addTime", Operators.GTE, start);
        }
        Date nowDate = DateUtil.getDate(System.currentTimeMillis() / 1000 + "");
        if (model.getTime() == 7) {
            param.addParam("addTime", Operators.GTE, DateUtil.rollDay(nowDate, -7));
            param.addParam("addTime", Operators.LTE, nowDate);
        } else if (model.getTime() > 0 && model.getTime() < 4) {
            param.addParam("addTime", Operators.GTE, DateUtil.rollMon(nowDate, -model.getTime()));
            param.addParam("addTime", Operators.LTE, nowDate);
        }
        if (StringUtil.isNotBlank(model.getEndTime())) {
            Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
            param.addParam("addTime", Operators.LTE, end);
        }
        if (model.getStatus() != 99) {
            param.addParam("status", model.getStatus());
        }
        return userCreditApplyDao.findPageList(param);
    }

    @Override
    public void save(UserCreditApply userCreditApply) {
        userCreditApplyDao.save(userCreditApply);
    }

    /**
     * 后台申请额度列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<UserCreditApplyModel> list(UserCreditApplyModel model) {
        QueryParam param = QueryParam.getInstance();
        if (StringUtil.isNotBlank(model.getSearchName())) {
            SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2);
        } else {
            if (StringUtil.isNotBlank(model.getUserName())) {
                param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
            }
            if (StringUtil.isNotBlank(model.getRealName())) {
                param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
            }
            if (model.getStatus() != 99 && model.getStatus() != 0) {
                param.addParam("status", model.getStatus());
            }
            if (!StringUtil.isBlank(model.getStartTime())) {
                param.addParam("addTime", Operators.GT, DateUtil.valueOf(model.getStartTime()));
            }
            if (!StringUtil.isBlank(model.getEndTime())) {
                Date date = DateUtil.valueOf(model.getEndTime());
                param.addParam("addTime", Operators.LT, DateUtil.rollDay(date, 1));
            }
        }
        param.addPage(model.getPage(), model.getRows());
        if ("desc".equals(model.getOrder())) {
            param.addOrder(OrderType.DESC, model.getSort());
        } else {
            param.addOrder(OrderType.ASC, model.getSort());
        }
        PageDataList<UserCreditApply> pList = userCreditApplyDao.findPageList(param);
        PageDataList<UserCreditApplyModel> modelList = new PageDataList<UserCreditApplyModel>();
        List<UserCreditApplyModel> list = new ArrayList<UserCreditApplyModel>();
        modelList.setPage(pList.getPage());
        if (pList.getList().size() > 0) {
            for (int i = 0; i < pList.getList().size(); i++) {
                UserCreditApply apply = pList.getList().get(i);
                UserCreditApplyModel uam = new UserCreditApplyModel();
                uam.setId(apply.getId());
                uam.setUserName(apply.getUser().getUserName());
                uam.setRealName(apply.getUser().getRealName());
                uam.setAccount(apply.getAccount());
                uam.setAccountOld(apply.getAccountOld());
                uam.setStatus(apply.getStatus());
                uam.setAddTime(apply.getAddTime());
                uam.setContent(apply.getContent());
                uam.setRemark(apply.getRemark());
                list.add(uam);
            }
        }
        modelList.setList(list);
        return modelList;
    }

    @Override
    public UserCreditApply findById(long id) {
        return userCreditApplyDao.find(id);
    }
}
